"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : list_hash二级分区表列约束:primary key
Create At   : 2022/2/17
Owner       : @daiguatutu
Description :
    1.创建二级分区表,二级分区键包含列约束主键
    2.查看索引类型
    3.创建二级分区表,一级分区键包含列约束主键
    4.查看索引类型
    5.插入数据
    6.插入重复数据
    7.清理环境
Expect      :
    1.成功
    2.成功,global类型
    3.成功
    4.成功,global类型
    5.成功
    6.合理报错
    7.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0140'

    def test_subpartition(self):
        text = '-----step1:创建二级分区表,二级分区键包含列约束主键 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int ,col_2 int primary key,col_3 int ,col_4 int) " \
            f"partition by list (col_1) subpartition by hash (col_2) " \
            f"( partition p_list_1 values (-1,-2,-3,-4,-5,-6,-7,-8,-9,-10) " \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3 ), " \
            f"  partition p_list_2 values (1,2,3,4,5,6,7,8,9,10) " \
            f"  ( subpartition p_hash_2_1 , subpartition p_hash_2_2 , " \
            f"    subpartition p_hash_2_3 , subpartition p_hash_2_4 , " \
            f"    subpartition p_hash_2_5 ), " \
            f"  partition p_list_3 values (11,12,13,14,15,16,17,18,19,20), " \
            f"  partition p_list_4 values (21,22,23,24,25,26,27,28,29,30 ) " \
            f"  ( subpartition p_hash_4_1 ), " \
            f"  partition p_list_5 values (default) " \
            f"  ( subpartition p_hash_5_1 ), " \
            f"  partition p_list_6 values (31,32,33,34,35,36,37,38,39,40) " \
            f"  ( subpartition p_hash_6_1 , subpartition p_hash_6_2 , " \
            f"    subpartition p_hash_6_3 )) enable row movement ; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:查看索引类型 expect:成功,global类型-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn('global partition index', msg, '执行失败:' + text)

        text = '-----step3:创建二级分区表,一级分区键包含列约束主键 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int primary key ,col_2 int ,col_3 int ,col_4 int) " \
            f"partition by list (col_1) subpartition by list (col_2)(" \
            f"partition p_list_1 values(-1,-2,-3,-4,-5,-6,-7,-8,-9,-10 )(" \
            f"subpartition p_list_1_1 values " \
            f"(0,-1,-2,-3,-4,-5,-6,-7,-8,-9 )," \
            f"subpartition p_list_1_2 values (default ))," \
            f"partition p_list_2 values(0,1,2,3,4,5,6,7,8,9)(" \
            f"subpartition p_list_2_1 values (0,1,2,3,4,5,6,7,8,9 )," \
            f"subpartition p_list_2_2 values (default )," \
            f"subpartition p_list_2_3 values " \
            f"(10,11,12,13,14,15,16,17,18,19)," \
            f"subpartition p_list_2_4 values " \
            f"(20,21,22,23,24,25,26,27,28,29)," \
            f"subpartition p_list_2_5 values " \
            f"(30,31,32,33,34,35,36,37,38,39))," \
            f"partition p_list_3 values(10,11,12,13,14,15,16,17,18,19)(" \
            f"subpartition p_list_3_2 values (default ))," \
            f"partition p_list_4 values(default )," \
            f"partition p_list_5 values(20,21,22,23,24,25,26,27,28,29)(" \
            f"subpartition p_list_5_1 values (0,1,2,3,4,5,6,7,8,9 )," \
            f"subpartition p_list_5_2 values (default )," \
            f"subpartition p_list_5_3 values " \
            f"(10,11,12,13,14,15,16,17,18,19)," \
            f"subpartition p_list_5_4 values " \
            f"(20,21,22,23,24,25,26,27,28,29)," \
            f"subpartition p_list_5_5 values " \
            f"(30,31,32,33,34,35,36,37,38,39))," \
            f"partition p_list_6 values(30,31,32,33,34,35,36,37,38,39)," \
            f"partition p_list_7 values(40,41,42,43,44,45,46,47,48,49)(" \
            f"subpartition p_list_7_1 values (default)))enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step4:查看索引类型 expect:成功,global类型-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn('global partition index', msg, '执行失败:' + text)

        text = '-----step5:插入数据 expect:成功-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(1,1,1,1);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step6:插入重复数据 expect:合理报错-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(1,1,1,1);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.unique_index_error_info, msg,
                      '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step7:清理环境-----')
        text_1 = '-----删除表 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)

        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg, '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
